//
//  URL+DBExtension.swift
//  DBCA
//
//  Created by mugua on 2024/7/6.
//

import Foundation

extension URL {
    
    var queryParameters: [String: Any] {
        
        var params = [String: Any]()
        
        guard let query = self.query else {
            return params
        }
        
        let pairs = query.components(separatedBy: "&")
        
        for pair in pairs {
            let elements = pair.components(separatedBy: "=")
            if elements.count == 2, let key = elements.first, let value = elements.last {
                let decodedKey = key.removingPercentEncoding ?? key
                let decodedValue = value.removingPercentEncoding ?? value
                
                if let intValue = Int(decodedValue) {
                    params[decodedKey] = intValue
                } else if let doubleValue = Double(decodedValue) {
                    params[decodedKey] = doubleValue
                } else {
                    params[decodedKey] = decodedValue
                }
            }
        }
        return params
    }
}
